package de.freakreactor.fatman;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
 * Represents a single map.
 * @author sba
 *
 */
public class Map {
	int[][] mapmatrix;
	int mapsizeX;
	int mapsizeY;
	String mapname;
	int ghostcount;
	int posFatmanX;
	int posFatmanY;
	int[] posGhostX;
	int[] posGhostY;
	ArrayList<Figure> Figures;
	
	
	/**
	 * Use to load a default map
	 */
	public Map()
	{
		
		//Set Hardcoded default testmap
		this.mapsizeX=26;
		this.mapsizeY=26;
		this.posFatmanX = 4;
		this.posFatmanY = 4;
		this.posGhostX[0] = 8;
		this.posGhostY[0] = 8;
		this.posGhostX[1] = 7;
		this.posGhostY[1] = 4;
		this.ghostcount = 2;
		this.mapname = "hardcoded debug map";
		for(int i=0;i<26;i++)
		{
			for(int j=0;j<26;j++)
			{
				mapmatrix[i][j]=1;
			}
		}
		
		this.createFigures();
		
	}
	/**
	 * Load map from file
	 * @param file
	 */
	public Map(String filename) 
	{   try {
		String[] split = null;
		File questionFile = new File(filename);
		FileReader fileReader = new FileReader(questionFile);
		BufferedReader reader = new BufferedReader(fileReader);
		String contentLine = null;
		
		// Read Mapsize
			if((contentLine = reader.readLine()) != null)
			{
			 split = contentLine.split(",");
			 this.mapsizeX = Integer.parseInt(split[0]);
			 this.mapsizeY = Integer.parseInt(split[1]);
			}
		// Read Mapname
			if((contentLine = reader.readLine()) != null)
			{
			this.mapname = contentLine;
			}
		// Read initPosition of Fatman
			if((contentLine = reader.readLine()) != null)
			{
			split = contentLine.split(",");
			this.posFatmanX = Integer.parseInt(split[0]);
			this.posFatmanY = Integer.parseInt(split[1]);
			}
		// Read initPosition of Ghosts
			if((contentLine = reader.readLine()) != null)
			{
			int j = 0;
			split = contentLine.split(",");
			this.ghostcount = split.length / 2;
		    for(int i=0;i<this.ghostcount;i++)
		    	{
				this.posGhostX[i] = Integer.parseInt(split[j]);
				this.posGhostY[i] = Integer.parseInt(split[j+1]);
				j=j+2;
		    	}
			}
	
	
		while ((contentLine = reader.readLine()) != null)
		{
		split = contentLine.split(",");	
		for(int y=0;y<this.mapsizeY;y++)
			{
			for(int x=0;x<this.mapsizeX;x++)
				{
				this.mapmatrix[x][y] = Integer.parseInt(split[x]);
				}
			}
		}
		
		reader.close();	
	    } catch (IOException e) {
			e.printStackTrace();
		}
	    
	    this.createFigures();
	    
	}
	
	/**
	 * Creates a list with all figures on this map and returns it.
	 * @return ArrayList<Figures>
	 */
	private ArrayList<Figure> createFigures()
	{ 
		if (!Figures.isEmpty()) {
			Figures.clear();
		}
		Figures.add(new FatMan());
		Figures.get(0).setPositionX(this.posFatmanX);
		Figures.get(0).setPositionY(this.posFatmanY);
		for (int i = 0; i < this.ghostcount; i++) {
			Figures.add(new Ghost());
			Figures.get(i+1).setPositionX(this.posGhostX[i]);
			Figures.get(i+1).setPositionY(this.posGhostY[i]);
		}
		return Figures;
	}
}
	
